<template>
  <div class="page-container">
    <el-card class="content-container">
      <div class="toolbar">
        <div class="toolbar-left">
          <h3>公告管理</h3>
        </div>
        <div class="toolbar-right">
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增公告
          </el-button>
        </div>
      </div>

      <!-- 搜索表单 -->
      <el-form :model="searchForm" class="search-form" inline>
        <el-form-item label="公告标题">
          <el-input v-model="searchForm.gonggaoName" placeholder="请输入公告标题" clearable />
        </el-form-item>
        <el-form-item label="公告类型">
          <el-select v-model="searchForm.gonggaoTypes" placeholder="请选择类型" clearable>
            <el-option label="系统公告" value="1" />
            <el-option label="活动公告" value="2" />
            <el-option label="维护公告" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="gonggaoName" label="公告标题" />
        <el-table-column prop="gonggaoTypesText" label="公告类型" width="120" />
        <el-table-column prop="gonggaoContent" label="公告内容" min-width="200">
          <template #default="scope">
            <div class="content-text">
              {{ scope.row.gonggaoContent }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="发布时间" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; text-align: right;"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const tableData = ref([])

const searchForm = reactive({
  gonggaoName: '',
  gonggaoTypes: ''
})

const pagination = reactive({
  page: 1,
  size: 20,
  total: 0
})

// 模拟数据
const mockData = [
  {
    id: 1,
    gonggaoName: '平台升级维护通知',
    gonggaoTypes: 3,
    gonggaoTypesText: '维护公告',
    gonggaoContent: '为了给用户提供更好的服务体验，平台将于2024年8月20日凌晨2:00-6:00进行系统升级维护，期间可能影响正常使用，敬请谅解。',
    createTime: '2024-08-16 10:30:00'
  },
  {
    id: 2,
    gonggaoName: '新用户注册优惠活动',
    gonggaoTypes: 2,
    gonggaoTypesText: '活动公告',
    gonggaoContent: '即日起至8月31日，新用户注册即可获得20元优惠券，首次购买满100元可使用，欢迎新用户加入我们的平台！',
    createTime: '2024-08-15 09:20:00'
  }
]

// 方法
const loadData = () => {
  loading.value = true
  
  // 模拟API调用
  setTimeout(() => {
    tableData.value = mockData
    pagination.total = mockData.length
    loading.value = false
  }, 500)
}

const handleSearch = () => {
  pagination.page = 1
  loadData()
}

const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

const handleAdd = () => {
  ElMessage.info('新增公告功能开发中...')
}

const handleEdit = (row) => {
  ElMessage.info(`编辑公告: ${row.gonggaoName}`)
}

const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除公告"${row.gonggaoName}"吗？`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('删除成功')
    loadData()
  })
}

const handleSizeChange = (size) => {
  pagination.size = size
  loadData()
}

const handleCurrentChange = (page) => {
  pagination.page = page
  loadData()
}

// 生命周期
onMounted(() => {
  loadData()
})
</script>

<style lang="scss" scoped>
@import '@/styles/index.scss';

.content-text {
  max-height: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  line-height: 1.4;
}
</style>
